package com.zeus.controller;

import com.zeus.pojo.Admins;
import com.zeus.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 冯扬龙
 * @date 2021/4/22 16:31
 */
@Controller
@RequestMapping("/admins")
public class AdminController {

    @Autowired
    private AdminService adminService;

    //    通过在浏览器地址栏输入地址http://localhost:8080/admins/loginui进入后台登录页面
    @RequestMapping("loginUi")
    public String loginUi() {
        return "login";
    }

    //    后台登录验证，拿到前端传的参数uname,password,自动会封装成admin对象,管理员不允许注册，直接手动把管理员存进数据库
    @RequestMapping("/login")
    public String login(String uname, String pwd,HttpServletRequest request) {
        Admins admins = adminService.login(uname, pwd);
        if(admins != null){
            HttpSession session = request.getSession();
            session.setAttribute("admins",admins);
            System.out.println(admins);
            return "redirect:/index";
        }else {
            return "error";
        }

    }
}
